﻿// <copyright file="CommonSectionHeader.cs" company="Sinclair Community College">
// Copyright 2010 Sinclair Community College
// </copyright>
// <originalAuthor>Patrick S. Seymour (patrick.seymour@gmail.com)</originalAuthor>

namespace SinclairCC.AppV
{
    using System;
    using System.Diagnostics.CodeAnalysis;

    /// <summary>
    /// This class represents the common section header in an App-V SFT/DSFT file.
    /// </summary>
    /// <remarks>
    /// All of the sections (both header metadata sections and data blocks) in the
    /// SFT file begin with a common header that is nine (9) bytes in size.
    /// </remarks>
    public class CommonSectionHeader
    {
        // TODO: Do we need the HeaderSize value?

        /// <summary>
        /// The size of the common section header, in bytes.
        /// </summary>
        public const byte HeaderSize = 9;

        /// <summary>
        /// The content type of the section.
        /// </summary>
        /// <remarks>
        /// In the SFT file, this is a 4-byte ASCII character string. See the "Common
        /// Section Header" section of the App-V SFT file format specification document
        /// for the list of defined type identifiers.
        /// </remarks>
        private string typeIdentifier;

        /// <summary>
        /// The total size (in bytes) for the entire header section.
        /// </summary>
        /// <remarks>
        /// The size is calculated starting from the beginning of the common section
        /// header until the last byte contained within the header section.
        /// </remarks>
        private int sectionSize;

        /// <summary>
        /// The revision number of the section.
        /// </summary>
        /// <remarks>
        /// This value is reserved and represents the version and format of the
        /// section's fields. See the "Common Section Header" section of the App-V SFT file
        /// format specification document for the list of defined revision numbers.
        /// </remarks>
        private byte revision;

        /// <summary>
        /// Initializes a new instance of the CommonSectionHeader class.
        /// </summary>
        /// <param name="headerData">
        /// Data that comprises the common section header.
        /// </param>
        public CommonSectionHeader(byte[] headerData)
        {
            byte[] typeIdBytes = new byte[4];
            Array.Copy(headerData, 0, typeIdBytes, 0, 4);
            this.typeIdentifier = System.Text.ASCIIEncoding.ASCII.GetString(typeIdBytes).ToUpperInvariant();

            this.sectionSize = BitConverter.ToInt32(headerData, 4);

            this.revision = headerData[headerData.Length - 1];
        }

        /// <summary>
        /// Gets the content type code for the section.
        /// </summary>
        public string TypeIdentifier
        {
            get { return this.typeIdentifier; }
        }

        /// <summary>
        /// Gets the size, in bytes, of the entire section.
        /// </summary>
        public int SectionSize
        {
            get { return this.sectionSize; }
        }

        /// <summary>
        /// Gets the revision number of the section.
        /// </summary>
        public byte Revision
        {
            get { return this.revision; }
        }
    }
}
